from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from scaner.api_v1.auth.dependencies import get_current_active_auth_user
from scaner.core.config.log_config import MyLogger
from scaner.core.models import User, db_helper
from . import crud
from . import schemas
from ..scrap.schemas import ADSDataRead


router = APIRouter(tags=["dashboard"])

main_logger = MyLogger("main")
logger = main_logger.getChild("dashboard")  # type: ignore


@router.get(
    "/",
    response_model=schemas.MainStatistics,
    status_code=status.HTTP_200_OK,
)
async def analytics_main(
    user: User = Depends(get_current_active_auth_user),
    session: AsyncSession = Depends(db_helper.get_async_session),
):
    return await crud.get_main_statistics(
        user,
        session,
    )


@router.get(
    "/workers/",
    response_model=list[schemas.ActiveTask],
    status_code=status.HTTP_200_OK,
)
async def analytics_workers(
    user: User = Depends(get_current_active_auth_user),
    session: AsyncSession = Depends(db_helper.get_async_session),
):
    return await crud.get_active_tasks_by_user_id(
        user,
        session,
    )


@router.get(
    "/ads/",
    response_model=list[ADSDataRead],
    status_code=status.HTTP_200_OK,
)
async def analytics_ads(
    user: User = Depends(get_current_active_auth_user),
    session: AsyncSession = Depends(db_helper.get_async_session),
):
    return await crud.get_last_five_ads_by_user_id(
        user,
        session,
    )


@router.get(
    "/chart/",
    response_model=schemas.DataForChart,
    status_code=status.HTTP_200_OK,
)
async def analytics_chart(
    user: User = Depends(get_current_active_auth_user),
    session: AsyncSession = Depends(db_helper.get_async_session),
):
    return await crud.get_data_for_chart(user, session)
